package de.wefunkdroid.core;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import de.wefunkdroid.core.data.Show;

public class WeFunkDroidActivity extends Activity {
	Streamplayer pl;
	String packet;
	Intent playerintent;
	Context ctx;
	private ArrayList<String> liste;
	private ImageAdapter imageAdapter;
	private Gallery gallery;
	BitmapManager bitmapman;

		public WeFunkDroidActivity() {
		packet = "de.wefunkdroid.core";
		liste = new ArrayList<String>();
	}

	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, 0, 0, "Jump");

		menu.add(0, 1, 0, "Search");
		
		menu.add(0, 2, 0, "Update");

		menu.add(0, 3, 0, "Exit");

		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			final AlertDialog.Builder alert = new AlertDialog.Builder(this);
			final EditText input = new EditText(this);
			alert.setView(input);
			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							goToShow(Integer.parseInt(input.getText()
									.toString().trim()));
						}
					});

			alert.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.cancel();
						}
					});
			alert.show();
			return true;
		case 1:
			final AlertDialog.Builder alertSearch = new AlertDialog.Builder(this);
			final EditText inputSearch = new EditText(this);
			alertSearch.setView(inputSearch);
			alertSearch.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							search(inputSearch.getText().toString());
						}
					});

			alertSearch.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.cancel();
						}
					});
			alertSearch.show();			
			return true;
		case 2:
			WefunkStatics.getInstance(ctx).update();
		case 3:
			this.finish();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void search(String string) {
		Pair<Show,Integer> r = WefunkStatics.getInstance(ctx).getHandler().search(string);
		Show show= r.first;
		int trackNum = r.second;
		goToTrack(show,trackNum);
	}
	
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(1);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ctx = this;
		Gallery g = (Gallery) findViewById(R.id.gallery1);
		g.setDrawingCacheEnabled(false);
		setLayoutAnim_slidedownfromtop(g);
		bitmapman = BitmapManager.getInstance(this);
		this.imageAdapter=new ImageAdapter(this, Parser.getShowsall());
		g.setAdapter(imageAdapter);
		g.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				playerintent = new Intent();
				playerintent.setClassName(packet, (new StringBuilder(String
						.valueOf(packet))).append(".").append("PlayerActivity")
						.toString());
				Toast.makeText(WeFunkDroidActivity.this,
						(new StringBuilder()).append(position+168).toString(), Toast.LENGTH_LONG)
						.show();
				if (Parser.getDateFromNum(String.valueOf(position + 168))
						.equals("")) {
					Toast.makeText(WeFunkDroidActivity.this,
							"Show does not exist - sorry!", 0).show();
				} else {
					playerintent.putExtra("Show", WefunkStatics
							.getInstance(ctx).getMp3Link(position));
					playerintent.putExtra("ShowNum", WefunkStatics.getInstance(
							ctx).getSchowNum(position));
					startActivity(playerintent);
				}
				viewanim(v);
			}
		});
		this.gallery = g;
		scroll(gallery,Parser.getLastShowNum()-168);
	}
	
	

	private void scroll(Gallery g, int pos) {
		g.setSelection(pos);
	}

	
	public void goToTrack(Show s, int trackNum) {
		int position = Integer.parseInt(s.getNum());
		playerintent = new Intent();
		playerintent.setClassName(packet, (new StringBuilder(String
				.valueOf(packet))).append(".").append("PlayerActivity")
				.toString());
		Toast.makeText(WeFunkDroidActivity.this,
				(new StringBuilder()).append(position+168).toString(),Toast.LENGTH_LONG).show();
		if (Parser.getDateFromNum(String.valueOf(position + 168)).equals("")) {
			Toast.makeText(WeFunkDroidActivity.this,
					"Show does not exist - sorry!", 0).show();
		} else {
			playerintent.putExtra("Show", WefunkStatics.getInstance(ctx)
					.getMp3Link(position));
			playerintent.putExtra("ShowNum", WefunkStatics.getInstance(ctx)
					.getSchowNum(position));
			playerintent.putExtra("TrackNum", String.valueOf(trackNum));
			startActivity(playerintent);
		}
	}
	
	public void goToShow(int pos) {
		int position = pos - 168;
		playerintent = new Intent();
		playerintent.setClassName(packet, (new StringBuilder(String
				.valueOf(packet))).append(".").append("PlayerActivity")
				.toString());
		Toast.makeText(WeFunkDroidActivity.this,
				(new StringBuilder()).append(position).toString(), 0).show();
		if (Parser.getDateFromNum(String.valueOf(position + 168)).equals("")) {
			Toast.makeText(WeFunkDroidActivity.this,
					"Show does not exist - sorry!", 0).show();
		} else {
			playerintent.putExtra("Show", WefunkStatics.getInstance(ctx)
					.getMp3Link(position));
			playerintent.putExtra("ShowNum", WefunkStatics.getInstance(ctx)
					.getSchowNum(position));
			startActivity(playerintent);
		}
	}
	
	

	public static void viewanim(View panel) {
		Animation animation = new ScaleAnimation(0.0F, 1.0F, 0.0F, 1.0F,
				(float) panel.getHeight() / 2.0F,
				(float) panel.getWidth() / 2.0F);
		animation.setDuration(250L);
		panel.setAnimation(animation);
		panel.startAnimation(animation);
	}

	protected void onDestroy() {
		super.onDestroy();
	}


	public static void setLayoutAnim_slidedownfromtop(ViewGroup panel) {
		AnimationSet set = new AnimationSet(true);
		Animation animation = new AlphaAnimation(0.0F, 1.0F);
		animation.setDuration(300L);
		set.addAnimation(animation);
		animation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, -1F, 1, 0.0F);
		animation.setDuration(1000L);
		set.addAnimation(animation);
		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.25F);
		panel.setLayoutAnimation(controller);
	}
	
	
	public static void setLayoutAnim_slidedownfromCenter(ViewGroup panel) {
		AnimationSet set = new AnimationSet(true);
		Animation animation = new AlphaAnimation(0.0F, 1.0F);
		animation.setDuration(300L);
		set.addAnimation(animation);
		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0F);
		animation.setDuration(1000L);
		set.addAnimation(animation);
		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.25F);
		panel.setLayoutAnimation(controller);
	}
	
	
	
	public class ImageAdapter extends BaseAdapter {

		public int getCount() {
			return liste.size();
		}

		public Object getItem(int position) {
			return Integer.valueOf(position);
		}

		public long getItemId(int position) {
			return (long) position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);
			Bitmap tmp = bitmapman.loadbitmap((String) liste.get(position));
			Canvas can = new Canvas(tmp);
			Paint paint = new Paint();
			paint.setStyle(Paint.Style.FILL);
			paint.setStrokeWidth(1);
			paint.setColor(Color.WHITE);
			paint.setTextSize(30);
			can.drawText("Show " + (position+168), 20, 22, paint);
			i.setImageBitmap(tmp);
			i.setLayoutParams(new android.widget.Gallery.LayoutParams(
							220, 220));
			i.setScaleType(android.widget.ImageView.ScaleType.CENTER_INSIDE);
			i.setBackgroundResource(mGalleryItemBackground);
			viewanim(i);
			//galleryAnim(parent);
			return i;
		}

		private void galleryAnim(ViewGroup gr){
			//AnimationSet set = new 
			
		}
		int mGalleryItemBackground;
		private Context mContext;

		public ImageAdapter(Context c, ArrayList<String> strlist) {
			mContext = c;
			liste = strlist;
			TypedArray a = obtainStyledAttributes(R.styleable.StationGallery);
			mGalleryItemBackground = a.getResourceId(0, 0);
			a.recycle();
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}
		
		
	}



}
